﻿#include "AF_MemManage.h"

namespace AF
{
#ifdef AF_SAFE_MEMORY
	MemManage g_MemManage;
	void* g_pPublicMem = NULL;


	MemManage::MemManage() :
		m_nTotalSize(0),
		m_nCount(0)
	{
#ifdef AF_DEBUG
		m_nIDMark = 0;
#endif
	}

	MemManage::~MemManage()
	{
		if(m_pHead != NULL)
		{
			DeleteAll();
		}
	}

#ifdef AF_DEBUG
	void* MemManage::New(BaseHand* pMemHand, size_t nMemSize, unsigned int nAryCount, MemType mt, const _STRING strClassName, const _STRING strFile, int nLine)
#else
	void* MemManage::New(BaseHand* pMemHand, size_t nMemSize, unsigned int nAryCount, MemType mt)
#endif
	{
		AF_CHECK_R(pMemHand != NULL, NULL);

		// 创建MemInfo，并为重要数据赋值
		MemInfo* _pmiNew = new((BYTE*)(pMemHand->GetPointer()) - sizeof(MemInfo)) MemInfo;
		_pmiNew->nValidate = AF_MEM_VALIDATE;

		_pmiNew->pHand = pMemHand;
		_pmiNew->nSize = nMemSize;
		_pmiNew->mt = mt;

		_pmiNew->pPrev = NULL;
		_pmiNew->pNext = NULL;

#ifdef AF_DEBUG
		// 为调试数据赋值

		// ID赋值
		_pmiNew->nMarkID = m_nIDMark++;
		if(_pmiNew->nMarkID == 50)
		{
			int a = 0;
		}

		_pmiNew->nCount = nAryCount;

		// 类名赋值
		_pmiNew->strClassName = strClassName;

		// 类名标记数组数量		
		if(nAryCount > 1)
		{
			char _szClassName[20];
			_SPRINTF(_szClassName, "_%d", nAryCount);
			_pmiNew->strClassName.append(_szClassName);
		}

		// 文件名赋值
		int _nStart1 = strFile.find_last_of('/');
		int _nStart2 = strFile.find_last_of('\\');
		int _nStart = AF_MAX_NUM(_nStart1, _nStart2) + 1;
		_pmiNew->strFile = strFile.substr(_nStart);

		// 行数赋值
		_pmiNew->nLine = nLine;
#endif /*AF_DEBUG*/

		// 添加至链表
		if(m_pEnd == NULL)
		{
			m_pHead = m_pEnd = _pmiNew;
		}
		else
		{
			m_pEnd->pNext = _pmiNew;
			_pmiNew->pPrev = m_pEnd;
			m_pEnd = _pmiNew;
		}

#ifdef AF_DEBUG
		// 添加至Map记录
		m_MemInfoMap.insert(MemInfoMap::value_type((MemInfoMapKey)_pmiNew->nMarkID, _pmiNew));
#endif

		m_nTotalSize += _pmiNew->nSize;
		m_nCount++;

		return pMemHand->GetPointer();
	}

	bool MemManage::Delete(void* pointer, MemType mt)
	{
		AF_CHECK_R(pointer != NULL, false);

		MemInfo* _pmiDelete = (MemInfo*)((BYTE*)pointer - sizeof(MemInfo));
		AF_CHECK_R(_pmiDelete->nValidate == AF_MEM_VALIDATE, false);

		// 单个与数组检查
		if(mt != MT_UNKNOW)
		{
			if(_pmiDelete->mt != mt)
			{
				AF_WARM("The Pointer Delete Error!", "");
			}
		}

		// 从链表中删除
		if(_pmiDelete == m_pHead)
		{
			m_pHead = _pmiDelete->pNext;
		}
		else
		{
			_pmiDelete->pPrev->pNext = _pmiDelete->pNext;
		}

		if(_pmiDelete == m_pEnd)
		{
			m_pEnd = _pmiDelete->pPrev;
		}
		else
		{
			_pmiDelete->pNext->pPrev = _pmiDelete->pPrev;
		}

		_pmiDelete->pPrev = NULL;
		_pmiDelete->pNext = NULL;

#ifdef AF_DEBUG
		// 清除记录
		MemInfoMap::iterator _cit = m_MemInfoMap.find((MemInfoMapKey)_pmiDelete->nMarkID);
		if(_cit != m_MemInfoMap.end())
			m_MemInfoMap.erase(_cit);
		else
			return false;
#endif

		m_nTotalSize -= _pmiDelete->nSize;
		m_nCount--;

		// 删除
		_pmiDelete->pHand->ReleasePointer();
		delete _pmiDelete->pHand;

		// 删除信息结构
		_pmiDelete->~MemInfo();

		// 释放内存
		free((BYTE*)_pmiDelete);

		return true;
	}

	void MemManage::DeleteAll()
	{
		while(m_pHead != NULL)
		{
			Delete((BYTE*)m_pHead + sizeof(MemInfo), MT_UNKNOW);
		}

#ifdef AF_DEBUG
		m_nIDMark = 0;
		m_MemInfoMap.clear();
#endif
		m_nTotalSize = 0;
		m_nCount = 0;
	}

	int MemManage::GetMemSize()
	{
		return m_nTotalSize;
	}

	int MemManage::GetHandCount()
	{
		return m_nCount;
	}
#endif /*AF_SAFE_MEMORY*/
}
